/**
 * 功能：将时间戳按照给定的 时间/日期 格式进行处理
 * @param {Number} date 时间戳 
 * @param {String} fmtExp 时间格式 'hh:ss'
 * @returns {String} 规范后的 时间/日期 字符串
 */
function fmtDate(date, fmtExp) {
  var date = new Date(date)

  var o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds() //毫秒
  };

  if (/(y+)/.test(fmtExp))
    fmtExp = fmtExp.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmtExp))
      fmtExp = fmtExp.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmtExp;
}
const filters = {
  formatDate(date) {
    // let arr = data.split(/[- / :]/);
    // let date = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
    let publishTime = new Date(date).getTime();
    let timeNow = new Date().getTime();
    let minute = 1000 * 60,
      hour = minute * 60,
      day = hour * 24,
      month = day * 30,
      year = month * 12;

    let d = timeNow - publishTime,
      d_days = parseInt(d / day),
      d_hours = parseInt(d / hour),
      d_minutes = parseInt(d / minute),
      d_seconds = parseInt(d);

    if (d_days > 0 && d_days < 3) {
      return d_days + '天前';
    } else if (d_days <= 0 && d_hours > 0) {
      return d_hours + '小时前';
    } else if (d_hours <= 0 && d_minutes > 0) {
      return d_minutes + '分钟前';
    } else if (d_seconds < 60) {
      if (d_seconds <= 0) {
        return '刚刚';
      } else {
        return d_seconds + '秒前';
      }
    } else if (d_days >= 3 && d_days < 30) {
      return fmtDate(publishTime, 'MM-dd');
    } else if (d_days >= 30) {
      return fmtDate(publishTime, 'yyyy-MM-dd')
    }
  }
}
export default (Vue) => {
  Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
  })
}
